package cn.ld.module.infra.dal.mapper.codegen;

import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.mybatis.core.mapper.BaseMapperX;
import cn.ld.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ld.module.infra.controller.admin.codegen.vo.table.CodegenTablePageInVO;
import cn.ld.module.infra.dal.entity.codegen.CodegenTableEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface CodegenTableMapper extends BaseMapperX<CodegenTableEntity> {

    default CodegenTableEntity selectByTableNameAndDataSourceConfigId(String tableName, Long dataSourceConfigId) {
        return selectOne(CodegenTableEntity::getTableName, tableName,
                CodegenTableEntity::getDataSourceConfigId, dataSourceConfigId);
    }

    default PageResult<CodegenTableEntity> selectPage(CodegenTablePageInVO pageInVO) {
        return selectPage(pageInVO, new LambdaQueryWrapperX<CodegenTableEntity>()
                .likeIfPresent(CodegenTableEntity::getTableName, pageInVO.getTableName())
                .likeIfPresent(CodegenTableEntity::getTableComment, pageInVO.getTableComment())
                .likeIfPresent(CodegenTableEntity::getClassName, pageInVO.getClassName())
                .betweenIfPresent(CodegenTableEntity::getCreateTime, pageInVO.getCreateTime())
                .orderByDesc(CodegenTableEntity::getUpdateTime)
        );
    }

    default List<CodegenTableEntity> selectListByDataSourceConfigId(Long dataSourceConfigId) {
        return selectList(CodegenTableEntity::getDataSourceConfigId, dataSourceConfigId);
    }

    default List<CodegenTableEntity> selectListByTemplateTypeAndMasterTableId(Integer templateType, Long masterTableId) {
        return selectList(CodegenTableEntity::getTemplateType, templateType,
                CodegenTableEntity::getMasterTableId, masterTableId);
    }

}
